package me.samlss.lighter_demo.slice;

import me.samlss.lighter.Lighter;
import me.samlss.lighter.animator.AnimatorAttr;
import me.samlss.lighter.animator.CustomAnimatorValue;
import me.samlss.lighter.interfaces.OnLighterListener;
import me.samlss.lighter.parameter.Direction;
import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.parameter.MarginOffset;
import me.samlss.lighter.shape.RectShape;
import me.samlss.lighter.util.LogUtil;
import me.samlss.lighter_demo.LighterHelper;
import me.samlss.lighter_demo.RecyclerViewAbility;
import me.samlss.lighter_demo.ResourceTable;
import me.samlss.lighter_demo.callback.OnItemClickListener;
import me.samlss.lighter_demo.tools.Toast;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class RecyclerViewAbilitySlice extends AbilitySlice {
    private DependentLayout recycler_root;
    private Image back_iv;
    private Text title_tv;
    private ListContainer recyclerview;
    private Adapter adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_recycler_view);
        initView();
        initListener();
    }

    private void initListener() {
        recycler_root.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                LogUtil.loge("touch:" + touchEvent.getAction());
                return true;
            }
        });
        recyclerview.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                LogUtil.loge("touch:" + touchEvent.getAction());
                return true;
            }
        });
        recyclerview.setScrolledListener(new Component.ScrolledListener() {
            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
                LogUtil.loge("Scroll:" + i);
            }
        });
    }

    private void initView() {
        recycler_root = (DependentLayout) findComponentById(ResourceTable.Id_recycler_root);
        title_tv = (Text) findComponentById(ResourceTable.Id_title_tv);
        if (getAbility() instanceof RecyclerViewAbility) {
            RecyclerViewAbility recyclerViewAbility = (RecyclerViewAbility) getAbility();
            title_tv.setText(recyclerViewAbility.titleName);
        }
        back_iv = (Image) findComponentById(ResourceTable.Id_back_iv);
        back_iv.setClickedListener(component -> getAbility().terminateAbility());
        recyclerview = (ListContainer) findComponentById(ResourceTable.Id_recyclerview);
        adapter = new Adapter(getContext());
        adapter.setListener((Component view, int postion) -> {
            showGuide(view);
        });

        recyclerview.setItemProvider(adapter);
    }

    private void showGuide(Component highlightedView) {
        if (highlightedView == null) {
            return;
        }

        Lighter.with(recycler_root, false)
                .setBackgroundColor(0x000000, 0.5f)
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedView(highlightedView)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_5)
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewDisplayAnimation(new CustomAnimatorValue(new AnimatorAttr()))
                        .setTipViewRelativeOffset(new MarginOffset(100, 0, 0, 0))
                        .build())
                .show();
    }


    @Override
    public void onActive() {
        super.onActive();
        Toast.show(getContext(), "Click to show highlight(点击显示高亮)~");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    public static class Adapter extends BaseItemProvider {
        private LayoutScatter mLayoutScatter;
        private OnItemClickListener listener;

        public Adapter(Context context) {
            this.mLayoutScatter = LayoutScatter.getInstance(context);
        }

        /**
         * 设置点击事件
         * @param listener listener
         */
        public void setListener(OnItemClickListener listener) {
            this.listener = listener;
        }

        @Override
        public int getCount() {
            return LighterHelper.sPictureList.size();
        }

        @Override
        public Object getItem(int i) {
            return LighterHelper.sPictureList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
            ViewHolder holder;
            if (component == null) {
                holder = new ViewHolder();
                component = mLayoutScatter.parse(ResourceTable.Layout_item_list, componentContainer, false);
                holder.item_root = component.findComponentById(ResourceTable.Id_item_root);
                holder.iv_image = (Image) component.findComponentById(ResourceTable.Id_iv_image);
                component.setTag(holder);
            } else {
                holder = (ViewHolder) component.getTag();
            }
            holder.iv_image.setPixelMap(LighterHelper.sPictureList.get(position));

            Component finalComponent = component;
            holder.item_root.setClickedListener((v) -> {
                if (listener != null) {
                    listener.onItemClick(finalComponent, position);
                }
            });
            return component;
        }
    }

    static class ViewHolder {
        Component item_root;
        Image iv_image;
    }


}
